<!--报警规则-->
<template>
  <div class="main">
    <div class="info-container">
      <div class="info-container_body">
        <el-form ref="refFrom" label-position="top" class="demo" v-loading="loading">
          <el-form-item label="规则名称">
            <el-input v-model="item.ruleName" />
          </el-form-item>
          <el-form-item label="报警类型">
            <el-select v-model="item.type" placeholder="报警类型">
              <el-option v-for="item in dict.station_error_reason" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="报警时限(小时)">
            <el-input-number :min="0" v-model="timeRange" />
          </el-form-item>
          <el-form-item label="报警次数">
            <el-input :min="0" v-model="item.times" />
          </el-form-item>
        </el-form>
      </div>
      <div class="info-container_footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSave">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { add, update } from '@/api/operation/alarmRule/index';
import common from '@/utils/common.ts';

export default {
  mounted() {
    const { proxy } = getCurrentInstance() as ComponentInternalInstance;
    this.proxy = proxy;
    this.dict = toRefs<any>(this.proxy?.useDict('station_error_reason'));

    this.init();
  },
  props: {
    data: {}
  },
  computed: {
    timeRange: {
      get() {
        return this.item.timeRange / 3600;
      },
      set(value) {
        this.item.timeRange = value * 3600;
      }
    }
  },
  data() {
    return {
      proxy: null,
      dict: {},
      loading: false,
      enabled: [
        { label: '停用', value: '0' },
        { label: '启用', value: '1' }
      ],
      type: [
        { label: '数据卡到期提醒', value: '1001' },
        { label: '站房接入量超限报警', value: '2001' },
        { label: '酒店接入量超限报警', value: '2002' },
        { label: '站房泄漏报警', value: '3001' },
        { label: '站房供氧能力不足报警', value: '3002' },
        { label: '漏测试结果通知', value: '3003' },
        { label: '站房氧气压力异常', value: '3004' },
        { label: '站房氧气浓度异常', value: '3005' },
        { label: '氧气储罐有效期到期提醒', value: '4001' },
        { label: '设备离线报警', value: '4002' },
        { label: '设备运行超限报警', value: '4003' },
        { label: '氧气储罐检测提醒', value: '4004' },
        { label: '站房泵头切换结果异常', value: '4005' },
        { label: '站房设备运行时长获取异常', value: '4006' },
        { label: '合同到期提醒', value: '5001' }
      ],
      item: {}
    };
  },
  created() {
    this.init();
  },
  methods: {
    init(data) {
      if (data == null) this.item = this.data;
      else this.item = data;
    },

    handleCancel() {
      this.$emit('hideRight');
    },

    handleSave() {
      let data = this.item;
      if (data.ruleId == null) {
        //新增
        this.add();
      } else {
        //编辑
        this.update();
      }
    },

    add() {
      this.loading = true;
      add(this.item)
        .then((res) => {
          this.loading = false;
          if (res.code == 200) {
            this.$emit('hideRight');
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    update() {
      this.loading = true;
      update(this.item)
        .then((res) => {
          this.loading = false;
          if (res.code == 200) {
            this.$emit('hideRight');
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {
          this.loading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.main {
  height: 100%;

  .contentFrm {
    padding: 10px;
    background: #f6f7fb;
  }

  .el-input-number__decrease,
  .el-input-number__increase {
    display: none;
  }

  .el-input-number {
    width: 100%;
  }
}
</style>